/*
 * @Author: underMoonzs 1994186472@qq.com
 * @Date: 2025-08-30 16:43:07
 * @LastEditors: underMoonzs 1994186472@qq.com
 * @LastEditTime: 2025-10-15 22:16:41
 * @FilePath: \lyshjpt\src\views\biz\utils\getData.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { getWaterQuality, getQushuikou } from "../api/water";
import { getPollutionSources } from "../api/pollution";
import { getRuhepaiwukou } from "../api/paiwukou";
import { getYujing } from "../api/yujing";

/**
 * 根据图层ID获取相应数据
 * @param {string} layerid - 图层ID，用于标识需要获取的数据类型
 * @returns {Promise} 返回包含数据的Promise对象
 */
export function getData(layerid, params) { 
  // 建立layerid到level的映射关系
  const waterLevelMap = {
    guokongzhan: "0",
    shengkongzhan: "1",
    shikongzhan: "2",
    xiankongzhan: "3",
  };
  if (waterLevelMap[layerid] !== undefined) {
    let query = {
      isauto: '1',
      isdrink:'0'
    };
    return getWaterQuality(query).then((response) => {
      // 为水质监测数据添加name字段（使用stationName）
      if (response.data) {
        response.data = response.data
          .filter((item) => item.controllevelcode == waterLevelMap[layerid])
          .map((i) => ({
            ...i,
            name: i.segmentname || i.name,
          }));
      }
      return response;
    });
  } else if (layerid === "shougongduanmian") {
    let query={
      ismanual:'1',
         isdrink:'0'

    }
    return getWaterQuality(query).then((response) => {
      if (response.data) {
        response.data = response.data.map((item) => ({
          ...item,
          name: item.segmentname || item.name || "实验室数据",
        }));
      }
      return response;
    });
  } else if (layerid === "shuiyuandiStation") {
      let query={
      ismanual:'1',
      isdrink:'1'

    }
    return getWaterQuality(query).then((response) => {
      // 为污染源数据添加name字段（使用enterpriseName）
      if (response.data) {
        response.data = response.data.map((item) => ({
          ...item,
          name: item.drinksourcename || item.name || "水源地水质监测点",
        }));
      }
      return response;
    });
  } else if (layerid === "qushuikou") {
    return getQushuikou().then((response) => {
      if (response.rows) {
        response.data = response.rows.map((item) => ({
          ...item,
          name: item.segmentname || item.name || "取水口",
        }));
      }
      return response;
    });
  } else if (layerid === "wuranyuan") {
    console.log("进入污染源数据");
    return getPollutionSources().then((response) => {
      if (response.data) {
        response.data = response.data.map((item) => ({
          ...item,
          name: item.polluteName || item.name || "污染源",
        }));
      }
      return response;
    });
  } else if (layerid === "wuranyuan_heatmap") {
    return getPollutionSources().then((response) => {
      // 为污染源数据添加name字段（使用enterpriseName）
      if (response.data) {
        response.data = response.data.map((item) => ({
          ...item,
          name: item.polluteName || item.name || "污染源",
        }));
      }
      return response;
    });
  } else if (layerid === "ruhepaiwukou") {
    return getRuhepaiwukou().then((response) => {
      // 为污染源数据添加name字段（使用enterpriseName）
      if (response.data) {
        response.data = response.data.map((item) => ({
          ...item,
          name: item.outfallname || item.name || "排污口",
        }));
      }
      return response;
    });
  } else if (layerid === "yujing") {
    return getYujing(params).then((response) => {

   if (response.data) {
        response.data = response.data.map((item) => ({
          ...item,
          name: item.pointName || item.name || "预警点",
          longitude: item.longitude || 0,
          latitude: item.latitude || 0,
        }))
      }

        return response;
    })
  }
  
  else {
    console.error("未知图层类型:", layerid);
    return Promise.resolve({ data: [] });
  }
}
